package com.qinghua.blog.controller.admin;

import com.qinghua.blog.po.Tag;
import com.qinghua.blog.po.Type;
import com.qinghua.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("admin")
public class TagController {

    @Autowired
    TagService tagService;

    //查询所有标签并且分页显示
    @GetMapping("/tags")
    public String tagList(@PageableDefault(size = 10,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){

        model.addAttribute("page",tagService.listTag(pageable));

        return "admin/tags";
    }
    //跳转到标签新增页面
    @GetMapping("/tags/input")
    public String showTagIns(Model model){
        //新建空对象，使新增页面不报异常
        Tag tag = new Tag();
        model.addAttribute("tag",tag);
        return "admin/tags_input";
    }
    @PostMapping("tags")
    public String tagIns(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
        Tag tagByName = tagService.getTagByName(tag.getName());
        if(tag.getName().equals(tagByName)){
            attributes.addFlashAttribute("msg","此类型已经存在");
            return "redirect:/admin/tags";
        }else {
            Tag tag1=tagService.saveTag(tag);
            if(tag1!=null){
                attributes.addFlashAttribute("msg","新增标签成功");
                return "redirect:/admin/tags";
            }else {
                attributes.addFlashAttribute("msg","新增标签失败");
                return "redirect:/admin/tags";
            }
        }
    }
    //跳转到修改页面
    @GetMapping("/tags/{id}/input")
    public String showUpdTags(@PathVariable Long id ,Model model){

        Tag tag = tagService.getTag(id);

        model.addAttribute("tag",tag);

        return "admin/tags_input";
    }
    @PostMapping("/tags/{id}")
    public String updTags(@Valid Tag tag, BindingResult result,@PathVariable Long id,RedirectAttributes attributes){
        Tag tagByName = tagService.getTagByName(tag.getName());
        if(tag.getName().equals(tagByName)){
            attributes.addFlashAttribute("msg","此类型已经存在");
            return "redirect:/admin/tags";
        }else {
            tag.setId(id);
            Tag tag1 = tagService.saveTag(tag);
            attributes.addFlashAttribute("msg","修改成功");
            return "redirect:/admin/tags";
        }
    }
    @GetMapping("/tags/{id}/delete")
    public String delTags(@PathVariable Long id,RedirectAttributes attributes){
        tagService.deleteTag(id);
        attributes.addFlashAttribute("msg","删除成功");
        return "redirect:/admin/tags";
    }

}
